package com.calligraphy.product.model;

import com.calligraphy.product.enums.ProductStatusEnum;
import com.calligraphy.product.utils.EnumUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.core.annotation.Order;

import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;

/**
 * @Author hy
 * @Date 2019/1/2 13:49
 **/
@Entity
@Data
@Table(name = "ProductInfo")
@DynamicUpdate
public class ProductInfo implements Serializable {

    private static final long serialVersionUID = 8174728741402602218L;

    @Id
    private String productId;

    /** 商品. */
    private String productName;

    /** 单价. */
    private BigDecimal productPrice;

    /** 库存. */
    private Integer productStock;

    /** 描述. */
    private String productDescription;

    /** 小图. */
    private String productIcon;

    /** 状态, 0正常1下架. */
    private Integer productStatus = ProductStatusEnum.UP.getCode();

    /** 类目编号. */
    private Integer categoryType;

    private Date createTime;

    private Date updateTime;

    @PrePersist
    public void onPersist() {
        createTime = new Timestamp(System.currentTimeMillis());
    }

    @PreUpdate
    public void onUpdate() {
        updateTime = new Timestamp(System.currentTimeMillis());
    }

    @JsonIgnore
    public ProductStatusEnum getProductStatusEnum(){
        return EnumUtil.getByCode(productStatus,ProductStatusEnum.class);
    }
}
